<?php
namespace app\farm\model;

use app\BaseModel;
use think\facade\Db;

class LandCate extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_land_cate';


    protected $append = [

        'farmer_id'
    ];


    /**
     * @param $value
     * @param $data
     * @功能说明:获取农场主
     * @author chenniang
     * @DataTime: 2021-12-10 17:23
     */
    public function getFarmerIdAttr($value,$data){

        if(!empty($data['id'])&&!empty($data['type'])){

            $text_model = new LandCateText();

            $dis = [

                'cate_id' => $data['id'],

                'type'    => $data['type']
            ];

            $list = $text_model->where($dis)->column('farmer_id');

            return array_values($list);
        }
    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        if(isset($data['farmer_id'])){

            $farmer_id = $data['farmer_id'];

            unset($data['farmer_id']);

        }

        $data['create_time'] = time();

        $res = $this->insert($data);

        $id = $this->getLastInsID();

        if(isset($farmer_id)){


            $this->updateSome($id,$data['uniacid'],$farmer_id,$data['type']);
        }

        return $id;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        if(isset($data['farmer_id'])){

            $farmer_id = $data['farmer_id'];

            unset($data['farmer_id']);

        }

        $res = $this->where($dis)->update($data);

        if(isset($farmer_id)){

            $this->updateSome($dis['id'],$data['uniacid'],$farmer_id,$data['type']);
        }

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-10 17:15
     * @功能说明:
     */
    public function updateSome($id,$uniacid,$data,$type){

        $text_model = new LandCateText();

        $text_model->where(['cate_id'=>$id,'type'=>$type])->delete();

        if(!empty($data)){

            foreach ($data as $k=>$v){

                $insert[$k] = [

                    'uniacid' => $uniacid,

                    'cate_id' => $id,

                    'farmer_id' => $v,

                    'type'     => $type
                ];

            }

            $text_model->saveAll($insert);
        }

        return true;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 17:47
     * @功能说明:认养分类
     */
    public function claimCateList($dis){

        $data = $this->alias('a')
                ->join('lbfarm_land_cate_text b','a.id = b.cate_id')
                ->where($dis)
                ->field('a.*')
                ->group('a.id')
                ->order('a.top desc.a.id desc')
                ->select()
                ->toArray();

        return $data;


    }







}